package action;

import bean.Project;
import com.opensymphony.xwork2.Action;

import dao.ProjectDAO;
import dao.ProjectDAOImpl;
import tool.HibernateSessionFactory;

import java.util.List;

public class ProjectAction implements Action {
    private ProjectDAO projectDAO = new ProjectDAOImpl(HibernateSessionFactory.getSession());
    private Project project;
    private List<Project> projects;

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public List<Project> getProjects() {
        return projects;
    }

    @Override
    public String execute() throws Exception {
        projects = projectDAO.queryAll();
        return SUCCESS;
    }

    public String add() {
        return projectDAO.add(project) ? "add" : null;
    }

    public String edit() {
        projects = projectDAO.queryByProjectId(project.getProjectId());
        project = projects.get(0);
        return "edit";
    }

    public String modify() {
        return projectDAO.modify(project) ? "modify" : null;
    }

    public String remove() {
        return projectDAO.remove(project.getProjectId()) ? "remove" : null;
    }

    public String queryByName() {
        projects = projectDAO.queryByProjectName(project.getProjectName());
        return "queryByName";
    }

    public String queryById() {
        projects = projectDAO.queryByProjectId(project.getProjectId());
        return "queryById";
    }
}
